package com.xbstar.datafusion.receiver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.xbstar.datafusion.controller.RuleManage;
import com.xbstar.datafusion.util.ObjectManage;
import com.xbstar.datafusion.util.PackageUtil;
import com.xbstar.datafusion.util.ProtocolType;

@Component
public class DataReceiver {
	@Autowired
	ObjectManage objectManage;
	@Autowired
	RuleManage ruleManage;
	 @KafkaListener(topics = "controllersend")
	    public void processMessage(String content) {
	        JSONObject json=JSONObject.parseObject(content);
	        String receiveId=json.getString("receiverId");
	        String blockId=json.getString("blockId");
	        objectManage.updateObjectByReceiveMsg(receiveId, json);
		 /**
		  * 如果影子死了，放入receive未接受队列
		  */
		 if(objectManage.canReceive(receiveId)){
	        	/**
	        	 * 根据接受规则进行转发（推送到具体系统）
	        	 * 接收方在规则中配置接受规则，即把标准块的字段转换为接收方设置的字段，按照设置的推送方式进行推送
	        	 * 推送后根据返回的参数或者推送状态更新影子状态
	        	 */
	        	
	        	JSONObject rejson=ruleManage.getPackageByBlockIdAndSysId(json,blockId,receiveId);
	        	ProtocolType receiveType=ruleManage.getProtocolTypeByBlockIdAndSysId(blockId,receiveId);
	        	ConnectSys connectSys=new ConnectSys();
				/**
				 * 注意：如果在sned失败的话，调用front方法告知发送失败。
				 */
				connectSys.send(PackageUtil.getOrinalData(rejson),receiveType);
	        }
	    }

}
